<?php
class Admin_AdminLanguageController extends Zendvn_Controller_Action{
	
	
	private $_paning = array('rowCount'=> 10, 'pageRange'=> 3);
	
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin/white');		
		$this->_templateObj->load();	
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		$this->_langObj->setLangFile(array('default.language.tmx'));		
		Zend_Registry::set('Zend_Translate',$this->_langObj->generate());
		
		/*====================================================
		 * Thiet lap cac gia tri cho session cua controller
		 *====================================================*/		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		//$ss->unsetAll();
		if(empty($ss->col)){
			$ss->col = 'id';
			$ss->order = 'DESC';
		}
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		
		/*====================================================
		 * Thiet lap gia tri cho phan phan trang cua controlle
		 *====================================================*/
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_paning['rowCount'] = $this->_configObj->getSite('list_limit');		
		$this->_paning['pageRange'] = 3;				
		$this->_arrParam['panigator'] = $this->_paning;
		
		
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function indexAction(){
	
		$this->view->Title = $this->view->translate('Language manager');
		$this->view->headTitle($this->view->Title,true);
		
		$tblLanguage = new Zendvn_Model_Table_Language();
		$this->view->Items = $tblLanguage->listItem($this->_arrParam);
		
		$totalItem = $tblLanguage->totalItem($this->_arrParam);
		
		$panigator = Zendvn_Paginator::create($this->_arrParam['panigator'],$totalItem);		
		$this->view->panigator = $panigator;		
	
		$this->view->slbSearchFiels = array('name'=>$this->view->translate('- Name field'),
											'code'=>$this->view->translate('- Code field'));
	}
	
	public function addAction(){
		
		$this->view->Title = $this->view->translate('Language manager') 
							 . " : " . $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		
		
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidateLanguage($this->_arrParam);
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item = $validator->getData();				
				//hien thoi loi
			}else{
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();		
				$tblLanguage = new Zendvn_Model_Table_Language();
				$tblLanguage->saveItem($data,array('task'=>'admin-add'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController . '/' . $this->_arrParam['action']);
				}
				//$this->_helper->viewRenderer->setNoRender();
			}
		}
		
		
		$this->_helper->viewRenderer('form');
	}
	
	public function editAction(){
		
		$this->view->Title = $this->view->translate('Language manager') 
							 . " : " . $this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		$tblLanguage = new Zendvn_Model_Table_Language();
		$this->view->Item = $tblLanguage->getItem($this->_arrParam);
		
		if($this->_request->isPost()){
			$validator = new Default_Form_ValidateLanguage($this->_arrParam);
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item = $validator->getData();	
			}else{
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));				
				$data =  $validator->getData();		
				$tblLanguage->saveItem($data,array('task'=>'admin-edit'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController 
									 . '/' . $this->_arrParam['action'] 
									 . '/id/' . $this->_arrParam['id']);
				}
			}
		}
		
		
		$this->_helper->viewRenderer('form');
	}
	
	public function statusAction(){
		$tblLanguage = new Zendvn_Model_Table_Language();
	
		if($this->_arrParam['type'] == 'multi'){
			$tblLanguage->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tblLanguage->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function defaultLanguageAction(){
		$tblLanguage = new Zendvn_Model_Table_Language();
	
		$tblLanguage->defaultLang($this->_arrParam);
		$this->_redirect($this->_mainAction);
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function infoAction(){
		$this->view->Title = $this->view->translate('Language manager') 
							 . " : " . $this->view->translate('Information');		
		$this->view->headTitle($this->view->Title,true);
		$tblLanguage = new Zendvn_Model_Table_Language();
		$this->view->Item = $tblLanguage->getItem($this->_arrParam);
	}
	
	public function sortAction(){		
		
		if($this->_request->isPost()){
			$tblLanguage = new Zendvn_Model_Table_Language();
			$tblLanguage->sortItem($this->_arrParam);
			$this->_redirect($this->_mainAction);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function deleteAction(){
		$this->view->Title = "Member :: Group manager :: Delete";
		$this->view->headTitle($this->view->Title,true);		
		$tblLanguage = new Zendvn_Model_Table_Language();
		
		if($this->_arrParam['type'] == 'multi'){
			$tblLanguage->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tblLanguage->deleteItem($this->_arrParam);
		}
		$this->_helper->flashMessenger->addMessage(array('success'=>'The delete was successful'));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
		
	}
	
	public function filterAction(){
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}
		
		if($this->_arrParam['type'] == 'filter'){
			$ss->status = $this->_arrParam['status'];
			$ss->area = $this->_arrParam['area'];
		}
		
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
}